import java.util.ArrayList;
import java.util.List;

public class StringMethod {
    public int indexOf(String source, String target) {
        if (source.length() < 1 || source == null || target.length() < 1 || target == null) return -1;
        if (target.length() > source.length()) return -1;
        List < Integer > startIndexList = startIndex(target.charAt(0), source);
        if (startIndexList.isEmpty()) return -1;
        for (Integer index: startIndexList) {
            if (index + target.length() > source.length()) continue;
            String temp = source.substring(index, index + target.length());
            if (temp.equals(target)) return index;
        }
        return -1;
    }

    public List<Integer> startIndex(char c, String source) {
        List < Integer > indexList = new ArrayList <> ();
        for (int i = 0; i < source.length(); i++) {
            if (source.charAt(i) == c) indexList.add(i);
        }
        return indexList;
    }

    public int indexOf2(String str1, String str2) {
        if (str1 == null || str1.length() < 1 || str2 == null || str2.length() < 1) {
            return -1;
        }
        if (str1.length() < str2.length()) {
            return -1;
        }
        char[] str_1 = str1.toCharArray();
        char[] str_2 = str2.toCharArray();
        for (int i = 0; i < str_1.length; i++) {
            boolean find = false;
            if (str_1[i] == str_2[0] && i + str_2.length <= str_1.length) {
                int equalcount = 1;
                for (int j = 1; j < str_2.length; j++) {
                    if (str_1[i + j] == str_2[j]) {
                        equalcount++;
                    }
                }
                if (equalcount == str_2.length) {
                    find = true;
                }
            }
            if (find) {
                return i;
            }

        }
        return -1;
    }



    public static void main(String[] args) {
        StringMethod stringMethod = new StringMethod();
        int index = stringMethod.indexOf2("acdddefb", "def");
        System.out.println(index);
    }
}
